package cn.tedu.live8.service.Impl;

import cn.tedu.live8.mapper.BrandMapper;
import cn.tedu.live8.pojo.dto.BrandAddNewDTO;
import cn.tedu.live8.pojo.dto.BrandUpdateDTO;
import cn.tedu.live8.pojo.entity.Brand;
import cn.tedu.live8.pojo.vo.BrandListVO;
import cn.tedu.live8.service.IBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class BrandServiceImpl implements IBrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public String addNew(BrandAddNewDTO brandAddNewDTO) {
        String title=brandAddNewDTO.getTitle();
        int count = brandMapper.countByTitle(title);
        if(count>0){
            return "添加失败,该商品标题已经存在";
        }

        Brand brand=new Brand();

        BeanUtils.copyProperties(brandAddNewDTO,brand);

        int insert = brandMapper.insert(brand);
        if(insert!=1){
            return "添加失败,请联系管理员";
        }
        return "添加成功";
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始删除id为:{}的数据.",id);
        brandMapper.deleteById(id);
    }

    @Override
    public void updateById(BrandUpdateDTO brandUpdateDTO) {
        log.debug("客户端发送的数据:{}",brandUpdateDTO);
        brandMapper.updateById(brandUpdateDTO);
    }

    @Override
    public List<BrandListVO> list() {
        List<BrandListVO> list = brandMapper.list();
        return list;
    }

}
